let selectedCell = null;

const cells = document.querySelectorAll('.cell');
const moveForm = document.getElementById('moveForm');
const fromXInput = document.getElementById('fromX');
const fromYInput = document.getElementById('fromY');
const toXInput = document.getElementById('toX');
const toYInput = document.getElementById('toY');

cells.forEach(cell => {
    cell.addEventListener('click', function () {
        if (!selectedCell) {
            if (this.classList.contains('piece')) {
                selectedCell = this;
                this.style.backgroundColor = 'yellow';
            }
        } else {
            const fromX = parseInt(selectedCell.dataset.x);
            const fromY = parseInt(selectedCell.dataset.y);
            const toX = parseInt(this.dataset.x);
            const toY = parseInt(this.dataset.y);

            fromXInput.value = fromX;
            fromYInput.value = fromY;
            toXInput.value = toX;
            toYInput.value = toY;

            moveForm.submit();
        }
    });
});